﻿using System;
using System.Collections.Generic;
using System.Management;
using System.Text;

//Not tested, error when used on machine w/o wifi

namespace Sensors
{
    class InternetSensor
    {
        const string wql_listar_adaptadores_conectados = "SELECT * FROM MSNdis_80211_BaseServiceSetIdentifier WHERE Active = True";
        const string wql_listar_redes_inalambricas = "SELECT * FROM MSNDis_80211_BSSIList";

        private static void WIFI_List()
        {
            ManagementScope managementscope = new ManagementScope("\\\\.\\root\\wmi");
            System.Management.ObjectQuery objectQuery = new System.Management.ObjectQuery(wql_listar_redes_inalambricas);
            ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(managementscope, objectQuery);
            ManagementObjectCollection moc = managementObjectSearcher.Get();
            ManagementObjectCollection.ManagementObjectEnumerator moe = moc.GetEnumerator();
            moe.MoveNext();
            ManagementBaseObject[] objarr = (ManagementBaseObject[])moe.Current.Properties["Ndis80211BSSIList"].Value;
            if (objarr != null)
            {
                foreach (ManagementBaseObject obj in objarr)
                {
                    char[] ssid = Encoding.ASCII.GetChars((byte[])obj["Ndis80211Ssid"]);
                    Console.Write("SSID: ");
                    int i;
                    for (i = 0; i < 32 && ssid[i] != '\0'; i++)
                    {
                        Console.Write(ssid[i]);
                    }
                    uint rs = (uint)obj["Ndis80211Rssi"];
                    Console.Write(".");
                    Console.WriteLine(" RSSI: " + rs);
                }
            }
        }

        public void run()
        {
            WIFI_List();
        }
    }
}
